package training.irpf;

import java.math.BigDecimal;

public class Irpf {
	
	public BigDecimal calculateIrpf(BigDecimal base){
		BigDecimal ret = new BigDecimal("0");
		BigDecimal aliquot;
		BigDecimal discount;
		
		if(base.compareTo(new BigDecimal("46939.56")) >= 0){
			aliquot = new BigDecimal("27.5");
			discount = new BigDecimal("8687.45");
			ret = calculateAliquot(base, aliquot).subtract(discount);
		}
		else if(base.compareTo(new BigDecimal("37566.13")) >= 0){
			aliquot = new BigDecimal("22.5");
			discount = new BigDecimal("6340.47");
			ret = calculateAliquot(base, aliquot).subtract(discount);
		}
		else if(base.compareTo(new BigDecimal("28174.21")) >= 0){
			aliquot = new BigDecimal("15.0");
			discount = new BigDecimal("3523.01");
			ret = calculateAliquot(base, aliquot).subtract(discount);
		}
		else if(base.compareTo(new BigDecimal("18799.33")) >= 0){
			aliquot = new BigDecimal("7.5");
			discount = new BigDecimal("1409.95");
			ret = calculateAliquot(base, aliquot).subtract(discount);
		}
		
		return ret;
	}
	
	private BigDecimal calculateAliquot(BigDecimal base, BigDecimal aliquot){

		BigDecimal divisor = new BigDecimal("100");
		return  (base.multiply(aliquot)).divide(divisor);
	}
}
